<?php

namespace App\Http\Requests\Admin\Config;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ChannelRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $where = [
            'game_id' => request()->get('game_id'),
            'channel_id' => request()->get('channel_id'),
        ];

        if (request()->method() == 'POST') {
            return [
                'game_id' => 'required',
                'channel_id' => [
                    'required',
                    Rule::unique('mc_game_channels')
                        ->where(function ($query) use ($where) {
                            return $query->where($where);
                        })
                ],
                'channel_name' => 'required',
                'server_ip' => 'required|ip',
                'port' => 'required|integer',
                'status' => 'required:integer',
            ];
        } elseif (request()->method() == 'PATCH') {
            return [
                'game_id' => 'required',
                'channel_id' => [
                    'required',
                    Rule::unique('mc_game_channels')
                        ->where(function ($query) use ($where) {
                            return $query->where($where)->where('id', '!=', request()->route('channel'));
                        })
                ],
                'channel_name' => 'required',
                'server_ip' => 'required|ip',
                'port' => 'required|integer',
                'status' => 'required:integer',
            ];
        }
    }

    /**
     * 提示信息s
     * @return array
     */
    public function messages()
    {
        return [
            'game_id.required' => '请选择游戏',
            'channel_id.required' => '请输入渠道ID',
            'channel_id.unique' => '渠道ID不能重复',
            'game_name.required' => '游戏名称不能为空',
            'server_ip.required' => '请填写服务器IP',
            'server_ip.ip' => '请填写正确的IP地址',
            'port.required' => '请填写服务器端口',
            'port.integer' => '服务器端口必须为整数',
            'status.required' => '状态不能为空',
            'status.integer' => '状态必须是数字',
        ];
    }
}
